package com.example.huhu.ch02;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.preference.DialogPreference;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;


public class MainActivity extends AppCompatActivity {
    //声明组件
    private Button register, cancel;
    private ToggleButton marriged;
    private RadioButton male, female;
    private EditText username, password;
    private Spinner position;
    private CheckBox reading, swimming;
    private ImageView imageView;
    private Bitmap bitmap;

    //menu 菜单的实现

    private static final int ITEM1 = Menu.FIRST;
    private static final int ITEM2 = Menu.FIRST + 1;
    private static final int ITEM3 = Menu.FIRST + 3;
    private static final int ITEM4 = Menu.FIRST + 4;
    private static final int ITEM5 = Menu.FIRST + 5;
    //声明文本视图
    private TextView myTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        male = (RadioButton) findViewById(R.id.male);
        female = (RadioButton) findViewById(R.id.female);
        reading = (CheckBox) findViewById(R.id.reading);
        swimming = (CheckBox) findViewById(R.id.swimming);
        marriged = (ToggleButton) findViewById(R.id.marriged);
        position = (Spinner) findViewById(R.id.position);
        imageView = (ImageView) findViewById(R.id.imageview);
        bitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.lf);
        imageView.setImageBitmap(bitmap);
        //根据ＩＤ获取文本视图
        myTV = (TextView) findViewById(R.id.color);
        //在文本视图中注册上下文菜单
        registerForContextMenu(myTV);








        //创建一个下拉列表选项数组
        String[] strs = {"CEO", "PM", "PL"};
        //创建一个数组适配器
        ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, strs);
        //设置下拉列表适配器
        position.setAdapter(aa);
        register = (Button) findViewById(R.id.register);
        cancel = (Button) findViewById(R.id.cancel);
        //注册监听
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("tag", "username:" + username.getText().toString());
                Log.i("tag", "password:" + password.getText().toString());
                if (male.isChecked()) {
                    Log.i("tag", "sex:male");
                } else {
                    Log.i("tag", "sex:female");
                }
                String temp = "like:";
                if (reading.isChecked()) {
                    temp += "read";
                }
                if (swimming.isChecked()) {
                    temp += "swim";


                }
                Log.i("tag", temp);
                if (marriged.isChecked()) {
                    Log.i("tag", "marriged:Yes");

                } else {
                    Log.i("tag ", "marriged:No");
                }
                Log.i("tag", "position:" + position.getSelectedItem().toString());


            }
        });
    }
    /**
     *  覆盖　onCreateOptionsMenu()方法。添加选项菜单
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //添加菜单项
        menu.add(0,ITEM1,0,"开始");
        menu.add(0,ITEM2,0,"退出");
        return true;

    }
    /**
     * 覆盖onOptionsItemSelected()方法，响应选项菜单被单击事件
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        //获得Builder对象
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        switch (item.getItemId())
        {
            //设置Activity 标题
            case ITEM1:
                setTitle("用户注册");
                break;
            case ITEM2:

                //通过Builder对象设置对话框信息
                builder.setMessage("真的要退出吗？")
                        //设置确定按钮及其响应事件处理
                .setPositiveButton("是",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int which){
                            //使用makeText（）方法创建Toast,其时长为Toast.LENGTH_LONG,即显示的时间长
                                Toast t1 = Toast.makeText(getApplicationContext(),"正在退出，请稍后",Toast.LENGTH_LONG);
                                t1.show();
                            }
                        })
                .setNegativeButton("否",
                        new DialogInterface.OnClickListener(){

                            public void onClick(DialogInterface dialog,int which){
                                Toast t2 = Toast.makeText(getApplicationContext(),"没有退出",Toast.LENGTH_SHORT);
                                t2.show();

                }



            });
                //创建对话框
                AlertDialog ad = builder.create();
                //显示对话框
                ad.show();
                break;

        }
        return true;
    }
    /**
     * 覆盖　onCreateContextMenu()方法，添加上下文菜单
     */
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        //添加菜单项
        menu.add(0,ITEM3,0,"红色背景");
        menu.add(0,ITEM4,0,"绿色背景");
        menu.add(0,ITEM5,0,"白色背景");

    }
    /**
     * 覆盖onContextItemSelected()方法，添加上下文菜单
     */
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case ITEM3:
                //设置文本视图的背景颜色
                myTV.setBackgroundColor(Color.RED);
                break;
            case ITEM4:
                //设置文本视图的背景颜色
                myTV.setBackgroundColor(Color.GREEN);
                break;
            case ITEM5:
                //设置文本视图的背景颜色
                myTV.setBackgroundColor(Color.WHITE);
                break;
        }
        return true;
    }
}
